import Api from '../service/Api';
import ChatManager from '../component/ChatManager';
import ChoicePage from './news_mass_choice';
import IosConfirm from '../component/IosConfirm';
import ModInput from '../component/ModInput';
import {showMessages} from '../chat';

export function group_member_page(groupId, groupName,re_id,back_btnType) {

  if (groupId >= 0) {
    return ;
  }
  
  const {User_ID, SessionID, Unit_ID, UserName} = JSON.parse(localStorage.getItem('yxtUser'));
  // 初始化组员信息
  Api.get(`/BasePlate/Interface/IInterfaceJson.asmx/GroupUser_GetList`, {
    User_ID, SessionID, Unit_ID,
    Group_ID: groupId.slice(1)
  }).then(resp => {
    if (resp && resp.Goodo && resp.Goodo.R) { // 得到组员信息
      const members = [].concat(resp.Goodo.R);

      const tpl = `
        <div class="top">
          <p>${groupName}（<span>${members.length}</span>）</p>
          <a class="re_discussion"><img src="img/news_public/news_sys.png"/>返回</a>
        </div>
        <div class="discussion_member">
          <ul>
            ${members.map(item => {
              return `<li><a><img src="${item.HeadPic ? Api.imageUrl(item.HeadPic) : 'img/news_public/user_6.png'}"/><span>${item.UserName}</span></a></li>`
            }).join('')}
            <li><a class="add_member"><img src="img/news_public/member_2.png"/><span></span></a></li>
          </ul>
          <a class="revise_name">群聊名称<span>${groupName}</span></a>
          <a class="quit_discussion">退出讨论组</a>
      </div>
      `

      $('.right').html(tpl);

      $('.right a.revise_name').click(() => {
        update_group_event({User_ID, Unit_ID, SessionID, Group_ID:groupId.slice(1), GroupName: groupName});
        ModInput.show({
          title: `修改群聊名称`,
          onReady: () => $('input.revise_inp').val(groupName)
        });
      });

      $('.right a.quit_discussion').click(() => {
        quit_group_event({User_ID, SessionID, Group_ID:groupId.slice(1)})
        IosConfirm.show(`确认退出讨论组？`);
      })

      $('.right a.add_member').click(() => {
        ChoicePage.render_mulpick({Group_ID:groupId.slice(1), GroupName: groupName, groupHasIds: members.map(item => item.User_ID)})
      })
      $(".right a.re_discussion").click(() =>{           //返回
							showMessages(re_id, back_btnType);
						})

    }
  })

}

function quit_group_event({User_ID, SessionID, Group_ID}) {

  IosConfirm.bindOk(() => {
    layer.msg(`退出中`, { time: 0 })
    Api.post(`/BasePlate/Interface/IInterfaceJson.asmx/GroupUser_Exit`, {
      User_ID, SessionID, Group_ID
    }).then(resp => {
      if (resp.Goodo.EID == '0') {
        layer.msg(`退出成功`, {
          time: 2000
        }, () => {
          $('body').css('pointer-events', 'all');
          ChatManager.removeMessage(`-${Group_ID}`);
          window.nonePage();
        })
      } else {
        layer.msg(`退出失败: ${resp.Goodo.ErrorMessage}`)
      }
    })
  })

}

function update_group_event({User_ID, Unit_ID, SessionID, GroupName, Group_ID}) {

  ModInput.bindOk((val) => {
    layer.msg(`修改中`, { time: 0 })
    Api.post(`/BasePlate/Interface/IInterfaceJson.asmx/Group_Edit`, {
      User_ID, Unit_ID, SessionID, GroupName: val, Group_ID
    }).then(resp => {
      if (resp.Goodo.EID == '0') {
        layer.msg(`修改成功`, {
          time: 2000
        }, () => {
          ChatManager.flushDiscussGroups();
          ChatManager.notifyMessage(`-${Group_ID}`);
          group_member_page(`-${Group_ID}`, val);
        })
      } else {
        layer.msg(`修改失败: ${resp.Goodo.ErrorMessage}`)
      }
    })
  })

}
